home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Prograph Classic 2.6.1 / Prograph Tutorial Manual / Prograph Tutorial / Prograph Tutorial.rsrc / TEXT_155.txt < prev    next >
Encoding:
Text File  |  1995-10-16  |  11.1 KB  |  238 lines

  1. t    Creating a Class-based Simulation
  2.  
  3. A discrete-event*269* simulation runs in time slices, from state to state. Most discrete-event simulations track the activity of individuals acting and reacting within an environment. Together, the entities acting within the environment along with the environment are described as a system. 
  4.  
  5. A discrete-event simulation might model a gas-station system‚Äîstudying the behavior of cars in and out of lines at the pumps. Or it might study the movement and interactions of molecules of gas in a jar. Your simulation looks at the behavior of animals in a barnyard.
  6.  
  7. In all these cases, it is intuitively clear that an object-oriented approach to programming*851* lends itself to explicit software modeling of the real-world entities described in such systems. So far, you have developed some of the basic data structures that describe the barnyard system. Now it is time to bring it to life with some methods and additional classes.
  8.  
  9. u    Open the Barnyard Attributes window and open a Value window on the activeYards class attribute. Click the Graphic check box. Delete three of the four string elements in the activeYards list. Double-click the remaining string element to open its Value window.
  10.  
  11. u    In the Value of List Item 1 window, scroll upward in the Data Types list and click the Barnyard item. The elementary data type string element is now transformed into an instance of class Barnyard.
  12.  
  13. u    Option-click the OK button in the Value window to update the attribute values and close both Value windows. The Barnyard Attributes window now shows a Barnyard instance as the first element in activeYards.
  14.  
  15. u    Execute the inspect yards universal method to confirm the change.
  16.  
  17. An interesting reflexive relationship occurs when a *93*class attribute holds instances of its own class. 
  18.  
  19. u    Open the Barnyard Attributes window. Open the Value of Attribute activeYards window. Next, open the Barnyard item in the activeYards list. The Barnyard instance‚Äôs own activeYards class attribute shows a list with the instance itself as the class attribute‚Äôs first item.
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. You can, in fact, get into an endless loop opening the activeYards attribute of each Value of List Item 1 window that you open in this manner. Here the activeYards class attribute has been opened three deep.
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. Opening such a reflexive relationship does not cause an error. Most likely, it is simply confusing and causes screen clutter. You do notice, however, that Prograph neatly stacks your progressive opening of Value windows. And as with all stacks of modal Value windows, an Option-click on the top window‚Äôs OK or Cancel button closes the entire stack.*1083*
  67.  
  68. The Application System class uses this reflexive construct. An instance of Application is stored in Application‚Äôs class attribute current. When you build an application in Application Builder, you are thus creating an instance stored in current. It is important for you to become comfortable inspecting such reflexive*941* object structures.
  69.  
  70. ===========================================================
  71. TIP:  The Value window titles are extremely helpful in avoiding a ‚Äúlooping inspection‚Äù of class attributes that have instances of their own class stored in them.
  72. ===========================================================
  73.  
  74. The direct inspection and editing of class structures, especially attribute default and instance values, is an important skill to refine as you become a Prographacker. The runtime debugging facilities you studied in the first tutorial are complemented by these class- and object-inspection and editing capabilities.
  75.  
  76. You continue to practice these object-inspection*768* and editing skills as you get to the business of creating a collection of animals to populate the Farmer‚Äôs barnyard. So you don‚Äôt get bogged down writing the event-processing loop that runs the simulation, refresh your skill at selectively loading files as you bring in some prewritten methods for the Barnyard Simulation.
  77.  
  78. The*973* Selective Load option is available in the standard file-opening dialog accessed by the Open‚Ķ item of the File menu. This option makes it easy to share and reuse classes and methods in your Prograph applications. Selective loading is context sensitive. The active window at the time you select Open‚Ķ determines whether classes or methods are loadable. 
  79.  
  80. u    Click to activate the Universal Methods window and select Open‚Ķ from the File menu. Click the Selective Load check box. Locate and select the file, Chapter 6 Helpers, in the Ch 6 Δí folder (inside the Tutorial Examples Δí folder). Press Return to open the Methods To Load dialog. Find, then Shift-click to select both the random from list and simulation methods in the scrolling list.
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. u    Press Return to load these universal methods. When Prograph is finished loading, you see these icons in the Universal Methods window.
  93.  
  94. u    Open the Methods window of the Barnyard class. Selectively load the Barnyard/action method from the Chapter 6 Helpers file.
  95.  
  96. You now have the simulation ‚Äúengine‚Äù in place. The three methods you just loaded implement the activity scenario laid out in the narrative description of the design goal. The simulation universal method endlessly repeats a local method, do next time slice, which simply tells each active Barnyard instance to take action.
  97.  
  98. The heart of the simulation, deciding what type of activity is to be performed and creating Diary entries, is done in the action method of Barnyard. If you are curious, you can inspect the inner workings of the action method to see how the narrative design goal was turned into a Prograph method. However, you need only know that it works. You can forge onward. To start running the Barnyard Simulation, you need two additional core classes:
  99.  
  100. u    Activate the Classes window and create a new class called DiaryEntry. Add a single attribute, what happened, and set its default data type to string. 
  101.  
  102. As each round of activity in a Barnyard instance begins, a DiaryEntry instance is created. As activity occurs, a descriptive sentence is appended to the what happened attribute of the DiaryEntry instance. At the end of a round of activity, the content of the current what happened attribute of the DiaryEntry is displayed for review in a Value window. Then the DiaryEntry instance is added to the end of the diary attribute of the active Barnyard. 
  103.  
  104. To see this in action, you need to start creating some barnyard animals.
  105.  
  106. u    Create a new class, Animal, in the Classes window. Give it the attributes, name, age, and food, with the following default values " " (an empty string), 1 (the simulation assumes all animals are at least a year old), and "generic chow", respectively.
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. Your animals know how to move, talk, eat, sleep, and have birthdays. To save you the trouble of creating methods to implement these behaviors, selectively load them, as follows:
  124.  
  125. u    Open the Animal Methods window and have it active before performing the selective load as usual. This ensures that the methods you select in the Methods To Load dialog are added to the Animal class.
  126.  
  127. u    Shift-click to select all methods prefixed with Animal/ in the dialog scrolling list, except the Animal/<<>> item (you get to that one later), and press Load.
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. The Barnyard Simulation is ready to go. Granted, you have a very undifferentiated kingdom, with all Barnyard creatures being simply animals‚Äînot cows and pigs or what have you. But it‚Äôs a start.
  140.  
  141. u    Select*668* Universal Methods *1122*from*1189* the Windows menu. Select the simulation method in the Universal window, and then choose Set Program from the Exec menu. This lets you easily run the Barnyard Simulation with a Command-R command.
  142.  
  143. Before running the simulation, you need to ‚Äúprime the pump‚Äù by putting a couple of Animal instances into the active Barnyard.
  144. u    Open the Barnyard Attributes window, and then open a Value window on the activeYards class attribute. Open the Barnyard instance that is Item 1 in the activeYards list.
  145.  
  146. u    Open a Value window on the inhabitants instance attribute of the Barnyard instance. Click Graphic and add three items to the inhabitants list.
  147.  
  148. u    Open each NULL item in the inhabitants list and select Animal to transform each into an Animal instance with attribute values such as:
  149.  
  150.  Item 1 Animal
  151.   name: Harvey
  152.   age: 7
  153.    food: pellets
  154.  
  155.  Item 2 Animal
  156.   name: Sally
  157.   age: 2
  158.    food: grass
  159.  
  160.  Item 3 Animal
  161.   name: Timmy
  162.   age: 3
  163.    food: oats and hay
  164.  
  165. When you have finished editing, the Value of Attribute inhabitants window looks like the following:
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. u    Option-click OK to close the Value windows, then start the simulation by pressing Command-R.
  191.  
  192. u    The simulation begins with a diary-entry description of what is going on in the barnyard at the time of the anthropologist‚Äôs first observation. The Value window you see is produced during execution of the Barnyard/action method when a display primitive receives the what happened attribute of the current DiaryEntry as input. (The activity description in your Value window might vary from the one below. The random from list method is used to determine the action each animal randomly performs at the time of observation.)
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. u    Press Return to proceed to the next round of events. Continue pressing Return and review the diary reports as they are produced.
  207.  
  208. Every fourth iteration of the Barnyard/action method skips the routine activity summary and produces the special birthday-and-shopping event noted at the outset description of the Barnyard Simulation. A series of four simple dialogs prompt you to find out what kind of animal the farmer bought at the auction.
  209.  
  210. u    To produce a new Animal instance to be added to the inhabitants of the Barnyard instance in activeYards, respond as follows:
  211.  
  212.  What kind of animal did the farmer buy?
  213.  
  214.    Animal
  215.  
  216.  What name did the farmer give the Animal?
  217.  
  218.    Albert
  219.  
  220.  Please enter the Animal‚Äôs age.
  221.  
  222.    2
  223.  
  224.  What is the Animal‚Äôs favorite food?
  225.  
  226.    oats and hay
  227.  
  228. The simulation creates the new Animal and adds it to the active Barnyard‚Äôs inhabitants attribute list. The Value window on the current DiaryEntry then reports on the animals‚Äô birthdays and on the acquisition of the new Animal at the auction. Continue running the simulation for a while, adding a new Animal each year. 
  229.  
  230. If you make a mistake and don‚Äôt type exactly the class name Animal in the What kind of animal did the farmer buy? dialog, an error message appears after the last dialog, noting that That class does not exist. Do you want to create it? Press Cancel and then Command-/ to abort execution. To start again, use Command-R. Run the simulation for a while, adding a new Animal each year. When you are ready to stop:
  231.  
  232. u    Use the*106* Break command (Command-.) immediately after dismissing a Value window displaying a DiaryEntry. The Stack window and an execution window on the currently executing method appear.
  233.  
  234. u    Select*10* Abort *329*from the Exec menu to stop running the simulation. The previously open editor windows reappear. Open the Barnyard instance in the activeYards list to inspect the content of the instance‚Äôs inhabitants and diary attributes.
  235.  
  236. Inspecting the inhabitants and diary attributes of the Barnyard instance in activeYards convinces you that the simulation is indeed working. 
  237.  
  238.